{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 1 2 0 -1 0 -1]\n",
      "epoch 0 sample 2 [1 0 2 -1 0 -1 -1]\n",
      "epoch 0 sample 3 [0 0 1 -2 1 1 1]\n",
      "epoch 1 sample 0 [0 1 2 -1 0 0 0]\n",
      "epoch 1 sample 1 [0 1 2 -1 0 0 0]\n",
      "epoch 1 sample 2 [1 1 2 -1 0 -1 -1]\n",
      "epoch 1 sample 3 [0 1 1 -2 1 1 1]\n",
      "epoch 2 sample 0 [0 2 2 -1 0 0 0]\n",
      "epoch 2 sample 1 [1 2 2 -1 -1 0 -1]\n",
      "epoch 2 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 2 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 3 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 4 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 5 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 6 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 7 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 8 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 3 [1 1 2 -2 0 0 0]\n",
      "epoch 9 sample 0 [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 1 [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 2 [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 3 [1 1 2 -2 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 0 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 0 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 1 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 1 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 1 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 1 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 2 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 2 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 2 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 2 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 3 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 3 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 3 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 3 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 4 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 4 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 4 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 4 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 5 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 5 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 5 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 5 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 6 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 6 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 6 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 6 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 7 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 7 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 7 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 7 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 8 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 8 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 8 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 8 sample 3 [1 1 2 0 0 0 0]\n",
      "epoch 9 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 9 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 9 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 9 sample 3 [1 1 2 0 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0 [0 1 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 1 2 0 0 0 0]\n",
      "epoch 0 sample 2 [1 1 2 0 0 0 0]\n",
      "epoch 0 sample 3 [1 1 2 0 -1 -1 -1]\n",
      "epoch 1 sample 0 [0 0 1 -1 0 0 0]\n",
      "epoch 1 sample 1 [0 0 1 -1 1 0 1]\n",
      "epoch 1 sample 2 [1 1 1 0 0 0 0]\n",
      "epoch 1 sample 3 [1 1 1 0 -1 -1 -1]\n",
      "epoch 2 sample 0 [0 0 0 -1 0 0 0]\n",
      "epoch 2 sample 1 [0 0 0 -1 1 0 1]\n",
      "epoch 2 sample 2 [0 1 0 0 0 1 1]\n",
      "epoch 2 sample 3 [1 1 1 1 -1 -1 -1]\n",
      "epoch 3 sample 0 [0 0 0 0 0 0 0]\n",
      "epoch 3 sample 1 [0 0 0 0 1 0 1]\n",
      "epoch 3 sample 2 [1 1 0 1 0 0 0]\n",
      "epoch 3 sample 3 [1 1 0 1 -1 -1 -1]\n",
      "epoch 4 sample 0 [0 0 -1 0 0 0 0]\n",
      "epoch 4 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 4 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 4 sample 3 [1 1 0 2 -1 -1 -1]\n",
      "epoch 5 sample 0 [1 0 -1 1 0 0 -1]\n",
      "epoch 5 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 5 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 5 sample 3 [1 1 0 2 -1 -1 -1]\n",
      "epoch 6 sample 0 [1 0 -1 1 0 0 -1]\n",
      "epoch 6 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 6 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 6 sample 3 [1 1 0 2 -1 -1 -1]\n",
      "epoch 7 sample 0 [1 0 -1 1 0 0 -1]\n",
      "epoch 7 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 7 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 7 sample 3 [1 1 0 2 -1 -1 -1]\n",
      "epoch 8 sample 0 [1 0 -1 1 0 0 -1]\n",
      "epoch 8 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 8 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 8 sample 3 [1 1 0 2 -1 -1 -1]\n",
      "epoch 9 sample 0 [1 0 -1 1 0 0 -1]\n",
      "epoch 9 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 9 sample 2 [0 1 -1 1 0 1 1]\n",
      "epoch 9 sample 3 [1 1 0 2 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 逻辑与数据\n",
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "#逻辑或数据\n",
    "\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "# 逻辑异或数据\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],\n",
    "]\n",
    "def perceptron(samples):\n",
    "    w = np.array([1,2])\n",
    "    b = 0 \n",
    "    a = 1\n",
    "    \n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:2])\n",
    "            y = 1 if np.dot(w,x) + b > 0 else 0\n",
    "            d = np.array(samples[j][2])\n",
    "            \n",
    "            delta_b = a*(d-y)\n",
    "            delta_w = a*(d-y)*x\n",
    "            \n",
    "            print('epoch {} sample {} [{} {} {} {} {} {} {}]'.format(i,j,y,w[0],w[1],b,delta_w[0],delta_w[1],delta_b))\n",
    "            w = w + delta_w\n",
    "            b = b + delta_b\n",
    "            \n",
    "if __name__ == '__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)\n",
    "    "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从结果可以看出，感知机对逻辑运算与和或能很快训练收敛得到正确的模型。但是对于异或问题则一直不能收敛.   \n",
    "这是由于感知器是一个线性模型，它是利用一个超平面来将数据进行分割的.    \n",
    "在直角坐标系中，从下图我们可以看到，与和或都能用一条直线将其分割，但是异或却不可能用一条直线进行分割。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSU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N1wc8m6tc0l61bpmLKubfCbe1f7leQXc/gHAty0wpoTk3wwyb/c4FlOSHJfknPyjTdvf8+yNf88h38owJ4RzPTiTA7HOXeGj9tTzrVs/e3Z+A8FWM19tSPJ26fnT83kEItTRjDX+5JcNT3/3ZkES83gcTw7R34D/j/K4R8K8Kczem4dbabtSQ4k+YFZzLIB/7ZR5J2sG3auZetlnawbYiZZN8wcsm7AuZatl3WyboiZjinrNnzoNfzjTpkG2Wemp8+Zbl9I8rbp+Vcl+WqSTyz5etEGzfOKJJ+eBsi10203JLlkev5pSd4zvdP/NMm3z+A+eqqZ/jjJXy65b/bO6LE76lzL1m544K3yvqok/yHJPUn+PMnlY7ivMvlEuw9Pw/ATSf7hDGZ6dyafEvnVTH5Ld3WS1yd5/ZL76ubpzH8+o8fvqWZ6W5JHljzXF2fx+A347xtN3sm64eZatlbWybohZpJ1s3tOyrpVzrVsrayTdUPMdExZV9NvBgAAgC6M6VOLAQAA4CkpsgAAAHRFkQUAAKAriiwAAABdUWQBAADoiiILAABAVxRZAAAAuqLIAgAA0JX/D5xheQyepTBEAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 1152x360 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "i = 1\n",
    "names = ['and','or','xor']\n",
    "plt.figure(figsize=(16,5))\n",
    "for s in [samples_and,samples_or,samples_xor]:\n",
    "    plt.subplot(1,3,i)\n",
    "    for p in s:\n",
    "            if p[-1] == 1:\n",
    "                plt.plot(p[0],p[1],marker='o',c='b')\n",
    "            else:\n",
    "                plt.plot(p[0],p[1],marker='x',c='r')\n",
    "    plt.xlim(-0.2,1.2)\n",
    "    plt.ylim(-0.2,1.2)\n",
    "    plt.title(names[i-1])\n",
    "    i += 1\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于异或这样的任务，需要非线性分类器来进行分割，这里提供两种方法：    \n",
    "- 使用多层感知机来构建非线性分类器\n",
    "- 将特征变换到高维，在高维空间中，线性可分      \n",
    "下面，就使用将特征变化到高维，这里我将增加一维特征，由已知的两个特征的乘积得到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 0] [1 2 2 0 -1 0 0]\n",
      "epoch 0 sample 2 [1 0] [0 2 2 -1 0 -1 0]\n",
      "epoch 0 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 1 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 1 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 1 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 1 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 2 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 2 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 2 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 2 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 3 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 4 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 5 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 6 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 7 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 8 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 0 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 1 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 2 [0 0] [0 1 2 -2 0 0 0]\n",
      "epoch 9 sample 3 [1 1] [0 1 2 -2 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 1 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 1 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 1 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 1 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 2 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 2 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 2 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 2 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 3 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 3 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 3 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 3 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 4 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 4 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 4 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 4 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 5 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 5 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 5 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 5 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 6 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 6 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 6 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 6 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 7 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 7 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 7 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 7 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 8 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 8 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 8 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 8 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 9 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 9 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 9 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 9 sample 3 [1 1] [1 2 2 0 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0 [0 0] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 1 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 2 [1 1] [1 2 2 0 0 0 0]\n",
      "epoch 0 sample 3 [1 0] [1 2 2 0 -1 -1 -1]\n",
      "epoch 1 sample 0 [0 0] [0 1 1 -1 0 0 0]\n",
      "epoch 1 sample 1 [0 1] [0 1 1 -1 1 0 0]\n",
      "epoch 1 sample 2 [1 1] [1 1 1 0 0 0 0]\n",
      "epoch 1 sample 3 [1 0] [1 1 1 0 -1 -1 -1]\n",
      "epoch 2 sample 0 [0 0] [0 0 0 -1 0 0 0]\n",
      "epoch 2 sample 1 [0 1] [0 0 0 -1 1 0 0]\n",
      "epoch 2 sample 2 [0 1] [1 0 0 0 0 1 0]\n",
      "epoch 2 sample 3 [1 0] [1 1 0 1 -1 -1 -1]\n",
      "epoch 3 sample 0 [0 0] [0 0 -1 0 0 0 0]\n",
      "epoch 3 sample 1 [0 1] [0 0 -1 0 1 0 0]\n",
      "epoch 3 sample 2 [1 1] [1 0 -1 1 0 0 0]\n",
      "epoch 3 sample 3 [1 0] [1 0 -1 1 -1 -1 -1]\n",
      "epoch 4 sample 0 [0 0] [0 -1 -2 0 0 0 0]\n",
      "epoch 4 sample 1 [0 1] [0 -1 -2 0 1 0 0]\n",
      "epoch 4 sample 2 [0 1] [1 -1 -2 1 0 1 0]\n",
      "epoch 4 sample 3 [1 0] [1 0 -2 2 -1 -1 -1]\n",
      "epoch 5 sample 0 [1 0] [0 -1 -3 1 0 0 0]\n",
      "epoch 5 sample 1 [0 1] [0 -1 -3 0 1 0 0]\n",
      "epoch 5 sample 2 [0 1] [1 -1 -3 1 0 1 0]\n",
      "epoch 5 sample 3 [0 0] [1 0 -3 2 0 0 0]\n",
      "epoch 6 sample 0 [1 0] [1 0 -3 2 0 0 0]\n",
      "epoch 6 sample 1 [1 1] [1 0 -3 1 0 0 0]\n",
      "epoch 6 sample 2 [1 1] [1 0 -3 1 0 0 0]\n",
      "epoch 6 sample 3 [0 0] [1 0 -3 1 0 0 0]\n",
      "epoch 7 sample 0 [1 0] [1 0 -3 1 0 0 0]\n",
      "epoch 7 sample 1 [1 1] [1 0 -3 0 0 0 0]\n",
      "epoch 7 sample 2 [0 1] [1 0 -3 0 0 1 0]\n",
      "epoch 7 sample 3 [0 0] [1 1 -3 1 0 0 0]\n",
      "epoch 8 sample 0 [1 0] [1 1 -3 1 0 0 0]\n",
      "epoch 8 sample 1 [1 1] [1 1 -3 0 0 0 0]\n",
      "epoch 8 sample 2 [1 1] [1 1 -3 0 0 0 0]\n",
      "epoch 8 sample 3 [0 0] [1 1 -3 0 0 0 0]\n",
      "epoch 9 sample 0 [0 0] [1 1 -3 0 0 0 0]\n",
      "epoch 9 sample 1 [1 1] [1 1 -3 0 0 0 0]\n",
      "epoch 9 sample 2 [1 1] [1 1 -3 0 0 0 0]\n",
      "epoch 9 sample 3 [0 0] [1 1 -3 0 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "# 逻辑与数据\n",
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "#逻辑或数据\n",
    "\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "# 逻辑异或数据\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],\n",
    "]\n",
    "\n",
    "def perceptron(samples):\n",
    "    w = np.array([1,2,2])\n",
    "    b = 0 \n",
    "    a = 1\n",
    "#     增加一维特征\n",
    "    samples = np.array(samples)\n",
    "    samples = np.insert(samples,2,values=samples[:,0]*samples[:,1],axis=1)\n",
    "    \n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:3])\n",
    "            y = 1 if np.dot(w,x) + b > 0 else 0\n",
    "            d = np.array(samples[j][3])\n",
    "            \n",
    "            delta_b = a*(d-y)\n",
    "            delta_w = a*(d-y)*x\n",
    "            \n",
    "            print('epoch {} sample {} [{} {}] [{} {} {} {} {} {} {}]'.format(i,j,y,d,w[0],w[1],w[2],b,delta_w[0],delta_w[1],delta_w[2],delta_b))\n",
    "            w = w + delta_w\n",
    "            b = b + delta_b\n",
    "            \n",
    "if __name__ == '__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，将特征扩展到高维后，异或也在第8轮迭代时收敛"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
